package com.ruoyi.oa.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.oa.domain.OaHumanEntry;
import com.ruoyi.oa.domain.vo.OaHumanEntryVo;
import com.ruoyi.oa.domain.bo.OaHumanEntryBo;
import com.ruoyi.oa.service.IOaHumanEntryService;
import com.ruoyi.common.core.page.TableDataInfo;


/**
 * 入职申请
 *
 * @author ruoyi
 * @date 2023-06-26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/oa/humanEntry")
public class OaHumanEntryController extends BaseController {

    private final IOaHumanEntryService iOaHumanEntryService;

    private final ISysUserService userService;
    /**
     * 查询入职申请列表
     */
    @SaCheckPermission("oa:employeeInfo:list")
    @GetMapping("/list")
    public TableDataInfo<OaHumanEntryVo> list(OaHumanEntryBo bo, PageQuery pageQuery) {
        return iOaHumanEntryService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出入职申请列表
     */
    @SaCheckPermission("oa:employeeInfo:export")
    @Log(title = "入职申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(OaHumanEntryBo bo, HttpServletResponse response) {
        List<OaHumanEntryVo> list = iOaHumanEntryService.queryList(bo);
        ExcelUtil.exportExcel(list, "入职申请", OaHumanEntryVo.class, response);
    }

    @Log(title = "入职申请", businessType = BusinessType.EXPORT)
    @PostMapping("/queryList")
    public R<List<OaHumanEntryVo>> queryList(OaHumanEntryBo bo, PageQuery pageQuery) {
        return R.ok(iOaHumanEntryService.queryList(bo));
    }


    /**
     * 获取入职申请详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("oa:employeeInfo:query")
    @GetMapping("/{id}")
    public R<OaHumanEntryVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(iOaHumanEntryService.queryById(id));
    }

    /**
     * 新增入职申请
     */
    @SaCheckPermission("oa:employeeInfo:add")
    @Log(title = "入职申请", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<OaHumanEntryBo> add(@Validated(AddGroup.class) @RequestBody OaHumanEntryBo bo) {
        SysUser user = new SysUser();
        user.setUserName(bo.getUserName());
        user.setNickName(bo.getNickName());
        user.setPhonenumber(bo.getPhonenumber());
        user.setEmail(bo.getEmail());
        if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(user))) {
            return R.fail("新增用户'" + user.getUserName() + "'失败，登录账号已存在");
        } else if (StringUtils.isNotEmpty(user.getPhonenumber())
            && UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(user))) {
            return R.fail("新增用户'" + user.getUserName() + "'失败，手机号码已存在");
        } else if (StringUtils.isNotEmpty(user.getEmail())
            && UserConstants.NOT_UNIQUE.equals(userService.checkEmailUnique(user))) {
            return R.fail("新增用户'" + user.getUserName() + "'失败，邮箱账号已存在");
        }
        return R.ok(iOaHumanEntryService.insertByBo(bo));
    }

    /**
     * 修改入职申请
     */
    @SaCheckPermission("oa:employeeInfo:edit")
    @Log(title = "入职申请", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<OaHumanEntry> edit(@Validated(EditGroup.class) @RequestBody OaHumanEntryBo bo) {
        return R.ok(iOaHumanEntryService.updateByBo(bo));
    }

    /**
     * 删除入职申请
     *
     * @param ids 主键串
     */
    @SaCheckPermission("oa:employeeInfo:remove")
    @Log(title = "入职申请", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iOaHumanEntryService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
